package org.jeecgframework.web.controller;

import com.mybatis.ext.WiseCoreService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ChainReportServiceI;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.system.service.UserService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 大伟 on 2017/1/3.
 * Description
 */
@Controller
@RequestMapping("chainReportController")
public class ZyChainReportController {

    @Autowired
    private WiseCoreService wiseCoreService;

    @Autowired
    private UserService userService;

    @Autowired
    private ChainReportServiceI chainReportService;

    @RequestMapping(params = "index")
    public ModelAndView goIndex(){
        ModelAndView mv = new ModelAndView("com/zybk/chain_report/report_gauge");
        TSUser tsUser = ResourceUtil.getSessionUserName();
        if(tsUser != null && userService.hasRole(tsUser.getUserName(), StaticValue.CHAIN_ROLE_LEADER)){
            mv.addObject("chain",1);
        }

        return mv;
    }

    @RequestMapping(params = "evaluate")
    public ModelAndView goEvaluate(){
        ModelAndView mv = new ModelAndView("com/zybk/chain_report/report_evaluate");
        TSUser tsUser = ResourceUtil.getSessionUserName();
        if(tsUser != null && userService.hasRole(tsUser.getUserName(), StaticValue.CHAIN_ROLE_LEADER)){
            mv.addObject("chain",1);
        }
        return mv;
    }


    /**
     * 判断用户是否属于院线用户，重新组装院线参数
     * @param param 待组装的map
     * @param request
     */
    public void assembleParam(Map param , HttpServletRequest request){
        TSUser tsUser = ResourceUtil.getSessionUserName();
        wiseCoreService.assembleParam(param,request);
        //如果系统已登录 并且 当前登录角色属于院线 并且 当前用户对应的depart信息不为空 时直接将院线的BPCode放入param
        if(tsUser != null && userService.hasRole(tsUser.getUserName(), StaticValue.CHAIN_ROLE_LEADER) && tsUser.getCurrentDepart() != null){
            param.put("chain",chainReportService.getChainBpCodeDic(tsUser.getCurrentDepart().getBpCode()));     //t_s_depart.bp_code = zy_equipment_spare.bp_no
        }else{
            String chain = oConvertUtils.getString(param.get("intheatername_1"));     //院线name
            if(StringUtil.isNotEmpty(chain)){
                param.put("intheatername_1",chain.replace(",","','"));
            }

        }
    }

    @RequestMapping(params = "drawGauge")
    @ResponseBody
    public JSONObject drawGauge(HttpServletRequest request){

        Map param = new HashMap();
        assembleParam(param,request);
        return chainReportService.drawMixGurava(param);
    }

    @RequestMapping(params = "drawRadaEvaluate")
    @ResponseBody
    public JSONObject drawRadaEvaluate(HttpServletRequest request){
        Map param = new HashMap();
        assembleParam(param,request);
        return chainReportService.drawEvaluateRada(param);
    }
}
